package com.ykdl.member.kid.adapters;

import java.util.ArrayList;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.beans.DoctorTalkChildListBean;
import com.ykdl.member.kid.gears.DoctorTalkChildListActivity;
import com.ykdl.member.kid.gears.HomeActivity;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.image.ImageUtil;
import com.ykdl.member.kid.models.ArticleBean;
import com.ykdl.member.kid.models.CatalogArticleItem;
import com.ykdl.member.kid.models.CatalogBean;
import com.ykdl.member.kid.models.CatalogItemBean;
import com.ykdl.member.kid.models.CatalogTopicItem;
import com.ykdl.member.kid.util.MobileUtils;
import com.ykdl.member.kid.widget.AsyncImageView;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.RelativeLayout.LayoutParams;

public class DoctorTalkChildListAdapter extends BaseAdapter {
//	private DoctorTalkChildListActivity activity;
	private Context activity;
	private ArrayList<Object>  lists;
	private BitmapUtils bitmapUtil;
	private int gridview_item_image_height = 0;
	public DoctorTalkChildListAdapter(Context activity, BitmapUtils  bitmapUtil, int gridview_item_image_height){
		this.activity = activity;
		this.bitmapUtil = bitmapUtil;
		this.gridview_item_image_height = gridview_item_image_height;
	}
	
	public void setList(ArrayList<Object>  beans){
		if(lists == null){
			lists = new ArrayList<Object>();
		}
		lists.addAll(beans);
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return lists.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return lists != null && lists.size() > 0 ? lists.get(position) : null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHold  hold = null;
		if (convertView == null){
			hold = new ViewHold();
			convertView = LayoutInflater.from(activity).inflate(R.layout.doctor_talk_child_item_view, null);
			hold.rl_doctor_talk_child = (RelativeLayout) convertView.findViewById(R.id.rl_doctor_talk_child);
			hold.iv_doctor_talk_child = (AsyncImageView) convertView.findViewById(R.id.iv_doctor_talk_child);
			
			LayoutParams   iv_heat_image_params = 	(LayoutParams) hold.iv_doctor_talk_child.getLayoutParams();
			iv_heat_image_params.width = LayoutParams.FILL_PARENT;
			iv_heat_image_params.height = this.gridview_item_image_height;
			hold.iv_doctor_talk_child.setLayoutParams(iv_heat_image_params);
			
			hold.tv_doctor_talk_child = (TextView) convertView.findViewById(R.id.tv_doctor_talk_child); //文本内容

			convertView.setTag(hold);
		}else{
			hold = (ViewHold) convertView.getTag();
		}
		
		if(lists == null)return null;
		 Object  obj = lists.get(position);
		 hold.iv_doctor_talk_child.setTag(position);
		if(obj instanceof ArticleBean){//文章
			 ArticleBean article = (ArticleBean)obj;
			if(article.getFile_meta() != null){
				 String url = article.getFile_meta().getDownload_urls().getBig().getUrl();
				 hold.iv_doctor_talk_child.setImageDrawable(null);
				 bitmapUtil.configDefaultLoadingImage(R.drawable.iask_main_middle_img);
				 bitmapUtil.configDefaultLoadFailedImage(R.drawable.iask_main_middle_img);
				 bitmapUtil.display(hold.iv_doctor_talk_child, url);
			}else{
				hold.iv_doctor_talk_child.setImageDrawable(null);
				hold.iv_doctor_talk_child.setImageResource(R.drawable.iask_main_middle_img);
			}
			
			ImageView supernatant = creatSupernatant(R.drawable.home_doctor_experts);
			hold.rl_doctor_talk_child.addView(supernatant);
			hold.tv_doctor_talk_child.setText(article.getTitle());
			
		}
		if(obj instanceof CatalogBean){//问医生
			 final CatalogBean  catalog = (CatalogBean)obj;
//			if(catalog.getCatalogItems() != null &&catalog.getCatalogItems().size() > 0){
				//if(catalog.getCatalog_type() == 2){  // 问题
					 if(catalog.getFile_meta() != null){
							final String url = catalog.getFile_meta().getDownload_urls().getBig().getUrl();
							hold.iv_doctor_talk_child.setImageDrawable(null);
							bitmapUtil.configDefaultLoadingImage(R.drawable.iask_main_middle_img);
							 bitmapUtil.configDefaultLoadFailedImage(R.drawable.iask_main_middle_img);
							bitmapUtil.display(hold.iv_doctor_talk_child, url);
						}else{
							hold.iv_doctor_talk_child.setImageDrawable(null);
							hold.iv_doctor_talk_child.setImageResource(R.drawable.iask_main_middle_img);
							
						}
					ImageView supernatant = creatSupernatant(R.drawable.home_doctor_answer_icon);
					hold.rl_doctor_talk_child.addView(supernatant);
					hold.tv_doctor_talk_child.setText(catalog.getTitle());
//					 CatalogItemBean  item = catalog.getCatalogItems().get(0);
//					if(item != null && !item.equals("{}")){
//						 CatalogTopicItem topic = (CatalogTopicItem)item;
//						if(topic.getItem() != null && !topic.getItem().equals("{}")){
//							if(topic.getItem().getFile_metas() != null){
//								 String url = topic.getItem().getFile_metas().get(0).getDownload_urls().getBig().getUrl();
//								hold.iv_doctor_talk_child.setImageDrawable(null);
//								bitmapUtil.configDefaultLoadingImage(R.drawable.iask_main_middle_img);
//								bitmapUtil.display(hold.iv_doctor_talk_child, url);
//							}else{
//								hold.iv_doctor_talk_child.setImageDrawable(null);
//								hold.iv_doctor_talk_child.setImageResource(R.drawable.iask_main_middle_img);
//								
//							}
//							ImageView supernatant = creatSupernatant(R.drawable.home_doctor_answer_icon);
//							hold.rl_doctor_talk_child.addView(supernatant);
//							hold.tv_doctor_talk_child.setText(topic.getItem().getTitle());
//						}
//					}
//				}else if(catalog.getCatalog_type() == 112){  // 文章
//						if(catalog.getFile_meta() != null){
//							 String url = catalog.getFile_meta().getDownload_urls().getBig().getUrl();
//							 hold.iv_doctor_talk_child.setImageDrawable(null);
//							 bitmapUtil.configDefaultLoadingImage(R.drawable.iask_main_middle_img);
//							 bitmapUtil.display(hold.iv_doctor_talk_child, url);
//						}else{
//							hold.iv_doctor_talk_child.setImageDrawable(null);
//							hold.iv_doctor_talk_child.setImageResource(R.drawable.iask_main_middle_img);
//							
//						}
//						ImageView supernatant = creatSupernatant(R.drawable.home_doctor_experts);
//						hold.rl_doctor_talk_child.addView(supernatant);
//						hold.tv_doctor_talk_child.setText(catalog.getTitle());
//					 CatalogItemBean  item = catalog.getCatalogItems().get(0);
//					if(item != null && !item.equals("{}")){
//						 CatalogArticleItem article = (CatalogArticleItem)item;
//						if(article.getItem() != null && !article.getItem().equals("{}")){
//							if(article.getItem().getFile_meta() != null){
//								 String url = article.getItem().getFile_meta().getDownload_urls().getBig().getUrl();
//								 hold.iv_doctor_talk_child.setImageDrawable(null);
//								 bitmapUtil.configDefaultLoadingImage(R.drawable.iask_main_middle_img);
//								 bitmapUtil.display(hold.iv_doctor_talk_child, url);
//							}else{
//								hold.iv_doctor_talk_child.setImageDrawable(null);
//								hold.iv_doctor_talk_child.setImageResource(R.drawable.iask_main_middle_img);
//								
//							}
//							ImageView supernatant = creatSupernatant(R.drawable.home_doctor_experts);
//							hold.rl_doctor_talk_child.addView(supernatant);
//							hold.tv_doctor_talk_child.setText(article.getItem().getTitle());
//						}
//					}
//				}
//			}
			
		}
		
		
		return convertView;
	}
	
	private ImageView creatSupernatant(int res) {
		 ImageView  supernatant = new ImageView(activity);
		 RelativeLayout.LayoutParams  sup_params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		sup_params.topMargin = MobileUtils.dpToPx(1);
		sup_params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
		supernatant.setLayoutParams(sup_params);
		supernatant.setBackgroundResource(0);
		supernatant.setBackgroundResource(res);
		return supernatant;
	}
	
	class ViewHold{
		 RelativeLayout rl_doctor_talk_child;
		 AsyncImageView iv_doctor_talk_child;
//		 ImageView  iv_sup;
		 TextView tv_doctor_talk_child;
	}

}
